# Fix Docker network connectivity issues
# This script helps resolve Docker Hub connection problems

$ErrorActionPreference = "Stop"

Write-Host "Fixing Docker network connectivity issues..." -ForegroundColor Green

# Check Docker status
Write-Host "`n1. Checking Docker status..." -ForegroundColor Yellow
try {
    $dockerInfo = docker info 2>$null
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✓ Docker is running" -ForegroundColor Green
    }
    else {
        Write-Host "✗ Docker is not running properly" -ForegroundColor Red
        Write-Host "Please start Docker Desktop and try again" -ForegroundColor Yellow
        exit 1
    }
}
catch {
    Write-Host "✗ Docker is not available" -ForegroundColor Red
    exit 1
}

# Test basic connectivity
Write-Host "`n2. Testing basic internet connectivity..." -ForegroundColor Yellow
try {
    $pingResult = Test-NetConnection -ComputerName "8.8.8.8" -Port 53 -InformationLevel Quiet
    if ($pingResult) {
        Write-Host "✓ Basic internet connectivity is working" -ForegroundColor Green
    }
    else {
        Write-Host "✗ Basic internet connectivity failed" -ForegroundColor Red
    }
}
catch {
    Write-Host "✗ Basic internet connectivity failed" -ForegroundColor Red
}

# Test Docker Hub connectivity
Write-Host "`n3. Testing Docker Hub connectivity..." -ForegroundColor Yellow
try {
    $dockerHubTest = Test-NetConnection -ComputerName "registry-1.docker.io" -Port 443 -InformationLevel Quiet
    if ($dockerHubTest) {
        Write-Host "✓ Docker Hub connectivity is working" -ForegroundColor Green
    }
    else {
        Write-Host "✗ Docker Hub connectivity failed" -ForegroundColor Red
        Write-Host "This is likely a network/firewall issue" -ForegroundColor Yellow
    }
}
catch {
    Write-Host "✗ Docker Hub connectivity failed" -ForegroundColor Red
}

# Configure Docker to use alternative registries
Write-Host "`n4. Configuring Docker to use alternative registries..." -ForegroundColor Yellow

# Create or update Docker daemon configuration
$dockerConfigPath = "$env:USERPROFILE\.docker\daemon.json"
$dockerConfigDir = Split-Path $dockerConfigPath -Parent

if (-not (Test-Path $dockerConfigDir)) {
    New-Item -ItemType Directory -Path $dockerConfigDir -Force | Out-Null
    Write-Host "✓ Created Docker config directory" -ForegroundColor Green
}

# Create Docker daemon configuration with registry mirrors
$dockerConfig = @{
    "registry-mirrors"    = @(
        "https://docker.mirrors.ustc.edu.cn",
        "https://hub-mirror.c.163.com",
        "https://mirror.baidubce.com"
    )
    "insecure-registries" = @()
    "debug"               = $false
    "experimental"        = $false
}

$dockerConfigJson = $dockerConfig | ConvertTo-Json -Depth 10
Set-Content -Path $dockerConfigPath -Value $dockerConfigJson -Encoding UTF8
Write-Host "✓ Updated Docker daemon configuration" -ForegroundColor Green
Write-Host "Configuration saved to: $dockerConfigPath" -ForegroundColor Cyan

# Show configuration
Write-Host "`nDocker daemon configuration:" -ForegroundColor Yellow
Get-Content $dockerConfigPath | Write-Host

# Restart Docker Desktop suggestion
Write-Host "`n5. Next steps:" -ForegroundColor Green
Write-Host "1. Restart Docker Desktop to apply the new configuration" -ForegroundColor White
Write-Host "2. Try building images again with: .\build-images.ps1" -ForegroundColor White
Write-Host "3. If still having issues, try using a VPN or different network" -ForegroundColor White

# Alternative: Use local images if available
Write-Host "`n6. Alternative solutions:" -ForegroundColor Green
Write-Host "• Use existing local images if available" -ForegroundColor White
Write-Host "• Try building with --network=host flag" -ForegroundColor White
Write-Host "• Use a different Docker registry" -ForegroundColor White

Write-Host "`nNetwork fix completed!" -ForegroundColor Green 